package cn.da.shuai.cool.system.api.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.system.model.entity.job.CoolJob;
import cn.da.shuai.cool.system.service.service.ICoolJobService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 调度任务信息操作处理
 */
@RestController
@RequestMapping("/monitor/job")
@Tag(name = "定时任务控制器")
public class CoolJobController {

    @Autowired
    private ICoolJobService jobService;

    @GetMapping("/list")
    @Operation(summary = "获取定时任务列表")
    public R<List<CoolJob>> list(CoolJob job) {
        List<CoolJob> list = jobService.selectJobList(job);
        return R.ok(list);
    }

    @GetMapping("/page")
    @Operation(summary = "获取定时任务分页列表")
    public R<Page<CoolJob>> page(CoolJob job, Page<CoolJob> page) {
        Page<CoolJob> list = jobService.selectJobPage(job, page);
        return R.ok(list);
    }

    @GetMapping("/{jobId}")
    @Operation(summary = "获取定时任务详细信息")
    public R<CoolJob> getInfo(@PathVariable("jobId") Long jobId) {
        return R.ok(jobService.selectJobById(jobId));
    }

    @PostMapping
    @Operation(summary = "新增定时任务")
    public R<Void> add(@RequestBody CoolJob job) throws SchedulerException {
        jobService.insertJob(job);
        return R.ok();
    }

    @PutMapping
    @Operation(summary = "修改定时任务")
    public R<Void> edit(@RequestBody CoolJob job) throws SchedulerException {
        jobService.updateJob(job);
        return R.ok();
    }

    @PutMapping("/changeStatus")
    @Operation(summary = "定时任务状态修改")
    public R<Void> changeStatus(@RequestBody CoolJob job) throws SchedulerException {
        jobService.changeStatus(job);
        return R.ok();
    }

    @PutMapping("/run")
    @Operation(summary = "定时任务立即执行一次")
    public R<Void> run(@RequestBody CoolJob job) throws SchedulerException {
        jobService.run(job);
        return R.ok();
    }

    @DeleteMapping("/{jobIds}")
    @Operation(summary = "删除定时任务")
    public R<Void> remove(@PathVariable Long[] jobIds) throws SchedulerException {
        jobService.deleteJobByIds(jobIds);
        return R.ok();
    }
}